package view
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import models.ModelLocator;
	
	import mx.core.Application;
	
	public class CanvasTip extends Sprite
	{
		[Embed(source="assets/tip/tipBg.png")]
		private var tipBgCls:Class;
		[Embed(source="assets/tip/mask.png")]
		private var tipMaskCls:Class;
		[Embed(source="assets/tip/zoom.png")]
		private var zoomCls:Class;
		
		private var bg:Bitmap;
		private var maskBitmap:Bitmap;
		private var zoomBitmap:Bitmap;
		private var zoomSprite:Sprite;
		private var loader:Loader;
		private var alphaMask:Bitmap;
		private var txt:TextField;
		private var sureBtn:ImageBtn;
		private var closeBtn:ImageBtn;
		private var imgBitmap:Bitmap;
		private var data:Object;
		
		private var showable:Boolean;
		
		
		public function CanvasTip()
		{
			init();
		}
		private function init():void{
			bg = new tipBgCls();
			this.addChild(bg);
			
			loader = new Loader()
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioError);
			loader.cacheAsBitmap = true;
			this.addChild(loader);
			
			maskBitmap = new tipMaskCls();
			maskBitmap.cacheAsBitmap = true;
			this.addChild(maskBitmap);
			
			loader.mask = maskBitmap;
			
			maskBitmap.x = loader.x = 40;
			maskBitmap.y = loader.y = 22;
			
			txt = new TextField();
			txt.width = 230;
			txt.height = 151;
			txt.multiline = true;
			txt.wordWrap = true;
			txt.textColor = 0x6c5939;
			txt.x = 106;
			txt.y = 75;
			txt.selectable = false;
			txt.filters = [new GlowFilter(0xffffff,1,2,2,3)]
			this.addChild(txt);
			
			sureBtn = new ImageBtn("选它");
			this.addChild(sureBtn);
			sureBtn.x = 106;
			sureBtn.y = 235;
			sureBtn.addEventListener(MouseEvent.CLICK,onSureClick);
			
			closeBtn = new ImageBtn("关闭");
			this.addChild(closeBtn);
			closeBtn.x = 216;
			closeBtn.y = 235;
			closeBtn.addEventListener(MouseEvent.CLICK,onMouseClick);
			
			zoomSprite = new Sprite;
			zoomBitmap = new zoomCls();
			zoomSprite.addChild(zoomBitmap);
			this.addChild(zoomSprite);
			zoomSprite.x = 72;
			zoomSprite.y = 216;
			zoomSprite.buttonMode = true;
			zoomSprite.addEventListener(MouseEvent.CLICK,onZoom);
		}
		private function onSureClick(event:MouseEvent):void{
			ModelLocator.getInstance().selectData = this.data;
			//PricePanle.getInstance().setPriceType(null,this.data);
			CanvasEdit(this.parent.parent).container.onClick();
		}
		private function onMouseClick(event:MouseEvent):void{
			this.hide();
		}
		public function show(data:Object,bitmap:Bitmap):void{
			this.data = data;
			this.addEventListener(Event.ENTER_FRAME,en);
			showable = true;
			loader.load(new URLRequest(data.detailPic));
			loader.scaleX = loader.scaleY = 0.3;
			txt.text = data.info;
			this.imgBitmap = bitmap;
		}
		public function hide():void{
			this.addEventListener(Event.ENTER_FRAME,en);
			showable = false;
			CanvasFrame.getInstance().runOver();
		}
		private function en(event:Event):void{
			if(showable){
				this.alpha += 0.05;
			}else{
				this.alpha -= 0.05;
			}
			if(this.alpha >= 1){
				this.mouseEnabled = true;
				this.mouseChildren = true;
				this.removeEventListener(Event.ENTER_FRAME,en);
				this.alpha = 1;
			}else if(this.alpha <= 0){
				this.mouseEnabled = false;
				this.mouseChildren = false;
				this.removeEventListener(Event.ENTER_FRAME,en);
				this.alpha = 0;
			}
		}
		private function ioError(event:IOErrorEvent):void{
			
		}
		private function onZoom(event:MouseEvent):void{
			CanvasFrame.getInstance().showDetailImg();
			CanvasFrame.getInstance().showDetail.show(data.detailPic);
				
		}
		
	}
}